//
//  EMDotSegControl.m
//  ElectronicMall
//
//  Created by 史伟文 on 15/1/31.
//  Copyright (c) 2015年 cczu. All rights reserved.
//

#import "EMDotSegControl.h"

@implementation EMDotSegControl
{
    UIView *segmentBackView;
    NSMutableArray *selectIndexArray;
    CGRect rect;
    UIImageView *gou;
    UIView *greenView;
    int chooseIndex;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        selectIndexArray = [NSMutableArray array];
        _titleArray = [NSArray array];
        rect = frame;
    }
    
    return self;
}
- (void)setTitleArray:(NSArray *)titleArray
{
    _titleArray = titleArray;
    chooseIndex = self.defaultIndex;
    [self initializeSegmentView];
}
- (void)initializeSegmentView
{
    
    segmentBackView = [[UIView alloc] initWithFrame:self.bounds];
    
    [self createSegmentItemWithNum:(int)_titleArray.count];
    
    
    
}

- (void)createSegmentItemWithNum: (int)num
{
    CGRect itemRect;
    
    itemRect.origin.y = 0;
    itemRect.size.width = rect.size.width / num;
    itemRect.size.height = rect.size.height;
    
    for (int i = 0; i < num; i ++) {
        itemRect.origin.x = i * (itemRect.size.width + 15);
        UIButton *icon = [UIButton buttonWithType:UIButtonTypeCustom];
        icon.frame = CGRectMake(itemRect.origin.x, itemRect.origin.y + 8, 15, 15);
        if (i == chooseIndex) {
            [icon setImage:[UIImage imageNamed:@"icon_购物车_需要发票"] forState:UIControlStateNormal];
        } else {
            [icon setImage:[UIImage imageNamed:@"icon_没选中"] forState:UIControlStateNormal];
        }
        
        [icon addTarget:self action:@selector(segmentValueChanged:) forControlEvents:
         UIControlEventTouchUpInside];
        icon.tag = 40300 + i;
        [self addSubview:icon];
        
        [selectIndexArray addObject:icon];
        
        itemRect.origin.x = i * (itemRect.size.width + 15) + 10;
        UIButton *segItem = [UIButton buttonWithType:UIButtonTypeCustom];
        segItem.frame = itemRect;
        [segItem setTitle:_titleArray[i] forState:UIControlStateNormal];
        [segItem setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        segItem.titleLabel.font = [UIFont fontWithName:HEITIMEDIUM size:12];
        [self addSubview:segItem];
    }
}
- (void)segmentValueChanged: (UIButton *)sender
{
    self.selectedIndex = sender.tag - 40300;
    for (int i = 0; i < selectIndexArray.count; i ++) {
        UIView *select = selectIndexArray[i];
        if (sender.tag == select.tag) {
            [sender setImage:[UIImage imageNamed:@"icon_购物车_需要发票"] forState:UIControlStateNormal];
            for (int j = 0; j < selectIndexArray.count; j ++) {
                if (j != i) {
                    UIButton *lastSelectButton = [selectIndexArray objectAtIndex:j];
                    [lastSelectButton setImage:[UIImage imageNamed:@"icon_没选中"] forState:UIControlStateNormal];
                }
            }
        }
    }
    if (self.actionBlock)
        self.actionBlock(self.selectedIndex);
}

@end
